#include <iostream>
#include <cmath>

using namespace std;
float fn(float);
float der(float);

int main()
{
    float x0,x1;
    const float re=0.00001;
    value:
          cout<<"Enter the value of x0:";
          cin>>x0;
    input:
          x1=x0-fn(x0)/der(x0);
          if(abs(fn(x1))>abs(fn(x0)))//use fabs insted of abs for improved accuracy
          {
              cout<<"The solution is diverging from roots"<<endl;
              goto value;
          }
          else if(abs((x1-x0)/x1)<=re)//use fabs insted of abs for improved accuracy
          {
              cout<<"Root="<<x1<<endl;
              cout<<"f("<<x1<<")="<<fn(x1);
          }
          else
          {
              x0=x1;
              goto input;
          }
          cin.get();
}

float fn(float x)
{
    float y;
    y=x*x-3*x-2;
    return(y);
}

float der(float x)
{
    float l;
    l=2*x-3;
    cout<<"der="<<l<<endl;
    return(l);
}
